package com.wxd.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.modules.sys.dao.SysUserDao;
import com.wxd.modules.sys.dao.SysUserRoleDao;
import com.wxd.modules.sys.entity.SysUserEntity;
import com.wxd.modules.sys.entity.SysUserRoleEntity;
import com.wxd.modules.sys.service.SysUserRoleService;
import com.wxd.modules.sys.service.SysUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户与角色对应关系
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRoleEntity> implements SysUserRoleService {
    @Override
    public void saveOrUpDate(Long userId, List<Long> roleIdList) {
        //1、先删除用户与角色对应关系
        this.remove(new QueryWrapper<SysUserRoleEntity>().eq("user_id",userId));
        if (roleIdList==null||roleIdList.size()==0)
        {
            return;
        }
        //2、保存用户与角色对应关系
        for(Long roleId:roleIdList)
        {
             SysUserRoleEntity sysUserRoleEntity=new SysUserRoleEntity();
             sysUserRoleEntity.setUserId(userId);
             sysUserRoleEntity.setRoleId(roleId);
             this.save(sysUserRoleEntity);
        }
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }
}
